home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / cp2dekit / samples / binfpak.cpp < prev    next >
C/C++ Source or Header  |  1996-12-29  |  3KB  |  154 lines

  1. //***************************************************************************
  2. //
  3. // this file is (c) '94-'96 Niklas Beisert
  4. //
  5. // this file is part of the cubic player development kit.
  6. // you may only use/modify/spread this file under the terms stated
  7. // in the cubic player development kit accompanying documentation.
  8. //
  9. //***************************************************************************
  10.  
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "binfile.h"
  15. #include "err.h"
  16. #include "psetting.h"
  17.  
  18. struct packdirentry
  19. {
  20.   char name[0x38];
  21.   long off;
  22.   long len;
  23. };
  24.  
  25. static int nfiles;
  26. static packdirentry *dir;
  27. static sbinfile packfile;
  28.  
  29. pakbinfile::pakbinfile()
  30. {
  31.   file=0;
  32. }
  33.  
  34. long pakbinfile::read(void *b, long l)
  35. {
  36.   if (!file)
  37.     return 0;
  38.   l=file->read(b, l);
  39.   filepos+=l;
  40.   return l;
  41. }
  42.  
  43. long pakbinfile::write(const void *b, long l)
  44. {
  45.   if (!file)
  46.     return 0;
  47.   l=file->write(b, l);
  48.   filepos+=l;
  49.   filelen=file->length();
  50.   return l;
  51. }
  52.  
  53. long pakbinfile::seek(long p)
  54. {
  55.   if (!file)
  56.     return 0;
  57.   p=file->seek(p);
  58.   filepos=p;
  59.   return p;
  60. }
  61.  
  62. long pakbinfile::chsize(long l)
  63. {
  64.   if (!file)
  65.     return 0;
  66.   l=file->seek(l);
  67.   filepos=file->tell();
  68.   filelen=file->length();
  69.   return l;
  70. }
  71.  
  72. void pakbinfile::close()
  73. {
  74.   if (file)
  75.   {
  76.     file->close();
  77.     delete file;
  78.   }
  79.   file=0;
  80. }
  81.  
  82. int pakbinfile::open(const char *name)
  83. {
  84.   close();
  85.  
  86.   sbinfile *sf=new sbinfile;
  87.   if (!sf)
  88.     return 0;
  89.   char path[_MAX_PATH];
  90.   strcpy(path, cfDataDir);
  91.   strcat(path, name);
  92.   if (!sf->open(path, sbinfile::openro))
  93.   {
  94.     delete sf;
  95.     int i;
  96.     for (i=0; i<nfiles; i++)
  97.       if (!stricmp(name, dir[i].name))
  98.         break;
  99.     if (i==nfiles)
  100.       return 0;
  101.     abinfile *f=new abinfile;
  102.     if (!f)
  103.       return 0;
  104.     if (!f->open(packfile, dir[i].off, dir[i].len))
  105.     {
  106.       delete f;
  107.       return 0;
  108.     }
  109.     file=f;
  110.   }
  111.   else
  112.     file=sf;
  113.  
  114.   mode=file->getmode();
  115.   filelen=file->length();
  116.   filepos=file->tell();
  117.   return 1;
  118. }
  119.  
  120. int pakfInit()
  121. {
  122.   char path[_MAX_PATH];
  123.   strcpy(path, cfDataDir);
  124.   strcat(path, "cp.pak");
  125.   nfiles=0;
  126.   dir=0;
  127.   if (!packfile.open(path, sbinfile::openro))
  128.     return errOk;
  129.   if (packfile.getl()!=0x4B434150)
  130.     return errOk;
  131.   int o=packfile.getl();
  132.   nfiles=packfile.getl()/0x40;
  133.   dir=new packdirentry[nfiles];
  134.   if (!dir)
  135.   {
  136.     nfiles=0;
  137.     return errGen;
  138.   }
  139.   packfile[o].read(dir, nfiles*0x40);
  140.   int i,j;
  141.   for (i=0; i<nfiles; i++)
  142.     for (j=0; j<0x38; j++)
  143.       if (dir[i].name[j]=='/')
  144.         dir[i].name[j]='\\';
  145.  
  146.   return errOk;
  147. }
  148.  
  149. void pakfClose()
  150. {
  151.   delete dir;
  152.   packfile.close();
  153. }
  154.